package com.gy.queue;

import java.util.Scanner;

public class CircleArrayQueueTest {

    public static void main(String[] args) {
        CircleArray1 circleArray1=new CircleArray1(4);
        char key=' ';
        Scanner scanner= new Scanner(System.in);
        boolean loop=true;
        //输出一个菜单
        while (loop){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列中取出数据");
            System.out.println("h(head):查看队列头的元素");
            key=scanner.next().charAt(0);//接收一个字符
            switch (key){
                case 's':
                    circleArray1.show();
                    break;
                case 'a':
                    System.out.println("请输入添加的数据：");
                    int value=scanner.nextInt();
                    circleArray1.add(value);
                    break;
                case 'g':
                    try {
                        int res=circleArray1.get();
                        System.out.printf("取出的数据是：%d\n",res);
                    }catch (Exception e){
                        System.out.println(e);
                    }
                    break;
                case 'h':
                    try {
                        int res=circleArray1.head();
                        System.out.printf("队列头的数据为：%d\n",res);
                    }catch (Exception e){
                        System.out.println(e);
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    System.out.println("程序已退出");
                    break;
                default:
                    break;
            }
        }
    }
}
class CircleArray1 {

    private int maxSize;
    private int front = 0;//指向队列第一个元素
    private int rear = 0;//指向队列最后一个元素的后一个空间
    private int[] arr;

    //构造
    public CircleArray1(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    //判断队列是否已满
    public boolean isFull() {
        if ((rear + 1) % maxSize == front) {
            System.out.println("队列已满！");
            return true;
        }
        return false;
    }

    //判断队列是否为空
    public boolean isNull() {
        if (rear == front) {
            System.out.println("队列为空！");
            return true;
        }
        return false;
    }

    //添加数据
    public void add(int data) {
        if (isFull()) {
            System.out.println("队列已满不能添加数据");
            return;
        }
        arr[rear] = data;
        rear = (rear + 1) % maxSize;
        System.out.printf("添加数据 %d 成功\n", data);
    }

    //取出数据
    public int get() {
        if (isNull()) {
            throw new RuntimeException("队列为空不能取出数据");
        }
        int num = arr[front];
        front = (front + 1) % maxSize;
        return num;
    }

    //判断数据个数
    public int number() {
        return (maxSize + rear - front) % maxSize;
    }

    //显示队列
    public void show() {
        if (isNull()) {
            System.out.println("队列为空,没有数据");
            return;
        }
        for (int i = front; i < front + number(); i++) {
            System.out.printf("arr[%d]=%d\n", i  % maxSize, arr[i % maxSize]);
        }
    }

    //显示第一个数据
    public int head() {
        if (isNull()) {
            System.out.println("队列为空,没有数据");
        }
        return arr[front];
    }
}

